import React from "react";
import BaseComponent from "../../../redare/components/BaseComponent";
import {Form, Input, Row, Tooltip} from "antd";
import SyncOutlined from "@ant-design/icons/lib/icons/SyncOutlined";
import DictionaryPresenter from "../../../.gen/presenter/DictionaryPresenter";

export default class DepartmentForm extends BaseComponent{

    formRef = React.createRef();

    constructor(props) {
        super(props);
        this._form.extraValue = {};
        this.state = {
            iconLoading: false,
            parentName: ''
        };
        this.presenter = new DictionaryPresenter(this)
    }

    componentDidMount() {
        this.getNewId()
    }

    getNewId() {
        this.setIconLoading(true);
        this.presenter.getNewDepartmentId(
            {},
            (result) => {
                this.setIconLoading(false);
                let {data} = result;
                this.formRef.current.setFieldsValue({departmentId: data})
            },
            () => {
                this.setIconLoading(false)
            }
        )
    }

    validateFormFields() {
        return new Promise((resolve, reject) => {
            this.formRef.current.validateFields().then((values) => {
                let resultValues = {...values, ...this._form.extraValue};
                resolve(resultValues);
            }).catch((errorInfo) => {
                reject(errorInfo);
            });
        });
    }

    setFormData(data) {
        if (this.formRef.current && data) {
            this.formRef.current.setFieldsValue(data);
        }
    }


    render() {
        let {data} = this.props;
        return (
            <Row>
                <Form ref={this.formRef}  className={'modal-form'}>
                    <Form.Item label="上级部门" >
                        <Input disabled={true} value={this.props.parentName}/>
                    </Form.Item>
                    <Form.Item label="部门编号" name="departmentId" >
                        <Input
                            disabled={true}
                            addonAfter={
                                ( !data || !data.id )
                                    ?
                                    <Tooltip title="刷新编号">
                                        <SyncOutlined onClick={() => this.getNewId()} spin={this.state.iconLoading}/>
                                    </Tooltip>
                                    :
                                    false
                            }
                        />
                    </Form.Item>
                    <Form.Item label="部门名称" name="departmentName" >
                        <Input placeholder="请输入部门名称"/>
                    </Form.Item>
                </Form>
            </Row>
        );
    }

    setIconLoading(iconLoading) {
        this.setState({iconLoading})
    }

}